﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ScannerApp.ScannerWebserviceWrapper;
using System.Collections;
using ScannerApp.Utils;
using CodeBetter.Json;

namespace ScannerApp.Forms
{
    public partial class QueryForm : ScannerBaseForm
    {
        private string currentBarCode = String.Empty;
        private int currentOrderIndex = 1;
        private int currentScanIndex = 1;
        public QueryForm()
        {
            InitializeComponent();
 
        }

        public override void Init()
        {
            base.Init();
            this.DispalyMessage("请扫描序列号,产品码,或者货位号");

        }

        public override void AssignControls()
        {
            this.lblMessage = this.labTitle;
            this.txtScanCode = this.txtScan;
            this.lblProductInfo = this.lblScanedInfo;
            this.lblHeaderInfo = this.lblHeader;
        }

        public override void HandleBarCode(string barCode)
        {
            try
            {
                base.HandleBarCode(barCode);
                if (barCode.Equals(string.Empty))
                {
                    return;
                }

                currentBarCode = barCode;
                QueryParameter param = getSearchParameter();
                
                ClearGridData();
                string strResult = ScannerWebServiceProxy.Instance.queryData(Converter.Serialize(param));
                BaseResult<QueryResult> serviceResult = Converter.Deserialize<BaseResult<QueryResult>>(strResult);
                if (serviceResult.IsSucessful())
                {
                    BindData(serviceResult.ReturnValue, "检测到扫描码对应信息.", param.pageSize);
                    if (!serviceResult.ReturnValue.IsQR)
                    {
                        this.mCurrentShelfCode = this.mCurrentProductQR;
                        this.mCurrentProductQR = string.Empty;
                    }
                    else
                    {
                        this.mCurrentShelfCode = string.Empty;
                    }
                    DispalyProductMessage();
                }
                else
                {
                    DispalyMessage(serviceResult.getReturnCodeMessage());
                    this.SetProductMessage(string.Empty, string.Empty, string.Empty,string.Empty);
                    DispalyProductMessage();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void ClearGridData()
        {
            this.dgOrder.TableStyles.Clear();
            this.dgOrder.DataSource = null;
            this.dgScan.TableStyles.Clear();
            this.dgScan.DataSource = null;
            lbOrderPagination.Text = String.Empty;
            lbScanPagination.Text = String.Empty;
            btnNext.Visible = false;
            btnPrev.Visible = false;
            btnScanNext.Visible = false;
            btnScanPrev.Visible = false;
        }

        private void BindData(QueryResult result, string message, int pageSize)
        {
            if (result.IsQR)
            {
                this.dgOrder.TableStyles.Clear();
                this.dgOrder.TableStyles.Add(Utils.Utils.GetTableStyle(typeof(QRStockInfo)));
                this.dgOrder.DataSource = Utils.Utils.ConvertListToDataTable(result.qrStockInfo);
            }
            else
            {
                RefreshOrderGrid(Utils.Utils.GetTableStyle(typeof(ProductStockInfo)), Utils.Utils.ConvertListToDataTable(result.productStockInfoList)
                    , result.OrderTotalPage, pageSize, result.OrderTotalCount);

                RefreshScanGrid(Utils.Utils.GetTableStyle(typeof(QRStockInfo)), Utils.Utils.ConvertListToDataTable(result.qrStockInfo)
                    , result.ScanTotalPage, pageSize, result.ScanTotalCount);
            }

            DispalyMessage(message);
        }

        private void RefreshOrderGrid(DataGridTableStyle style, DataTable dataTable, int totalPage,int pageSize, int totalCount)
        {
            this.dgOrder.TableStyles.Clear();
            this.dgOrder.TableStyles.Add(style);
            this.dgOrder.DataSource = dataTable;
            if (this.currentOrderIndex == 1)
            {
                this.btnPrev.Enabled = false;
            }
            else
            {
                this.btnPrev.Enabled = true;
            }

            if (this.currentOrderIndex == totalPage)
            {
                this.btnNext.Enabled = false;
            }
            else
            {
                this.btnNext.Enabled = true;
            }
            this.lbOrderPagination.Text = "第" + this.currentOrderIndex + "/" + totalPage + "页," + "每页" + pageSize + "条," + "共" + totalCount + "条";
            this.btnPrev.Visible = true;
            this.btnNext.Visible = true;
        }

        private void RefreshScanGrid(DataGridTableStyle style, DataTable dataTable, int totalPage, int pageSize, int totalCount)
        {
            this.dgScan.TableStyles.Clear();
            this.dgScan.TableStyles.Add(style);
            this.dgScan.DataSource = dataTable;

            if (this.currentScanIndex == 1)
            {
                this.btnScanPrev.Enabled = false;
            }
            else
            {
                this.btnScanPrev.Enabled = true;
            }

            if (this.currentScanIndex == totalPage)
            {
                this.btnScanNext.Enabled = false;
            }
            else
            {
                this.btnScanNext.Enabled = true;
            }
            this.lbScanPagination.Text = "第" + this.currentScanIndex + "/" + totalPage + "页," + "每页" + pageSize + "条," + "共" + totalCount + "条";
            this.btnScanPrev.Visible = true;
            this.btnScanNext.Visible = true;
        }

        private void txtScan_KeyPress(object sender, KeyPressEventArgs e)
        {
            this.HandleKeyPress(e);
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            this.currentOrderIndex++;
            batchExtantAmtPagination();
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            this.currentOrderIndex--;
            batchExtantAmtPagination();
        }

        private void btnScanPrev_Click(object sender, EventArgs e)
        {
            this.currentScanIndex--;
            this.qrExtantAmtPagination();
        }

        private void btnScanNext_Click(object sender, EventArgs e)
        {
            this.currentScanIndex++;
            this.qrExtantAmtPagination();
        }

        private QueryParameter getSearchParameter()
        {
            QueryParameter param = new QueryParameter();
            Hashtable result = Utils.Utils.CheckBarcodeType(currentBarCode);
            param.barcodeType = (int)result[CheckBarcodeKeys.BarcodeType];

            if (result[CheckBarcodeKeys.BarcodeType].Equals(ProductBarcodeType.HIBC_First) || result[CheckBarcodeKeys.BarcodeType].Equals(ProductBarcodeType.GS1_First))
            {
                param.barcode = result[CheckBarcodeKeys.ProductCode].ToString();
                this.SetProductMessage(result[CheckBarcodeKeys.ProductCode].ToString(), string.Empty, string.Empty);
            }
            else if (result[CheckBarcodeKeys.BarcodeType].Equals(ProductBarcodeType.HIBC_Second) || result[CheckBarcodeKeys.BarcodeType].Equals(ProductBarcodeType.GS1_Second))
            {
                param.barcode = result[CheckBarcodeKeys.Lot].ToString();
                this.SetProductMessage(string.Empty, result[CheckBarcodeKeys.Lot].ToString(), string.Empty);

            }
            else if (result[CheckBarcodeKeys.BarcodeType].Equals(ProductBarcodeType.GS1_Complete))
            {
                param.barcode = result[CheckBarcodeKeys.ProductCode] + "|" + result[CheckBarcodeKeys.Lot];
                this.SetProductMessage(result[CheckBarcodeKeys.ProductCode].ToString(), result[CheckBarcodeKeys.Lot].ToString(), string.Empty);
            }
            else if (result[CheckBarcodeKeys.BarcodeType].Equals(ProductBarcodeType.QR))
            {
                param.barcode = currentBarCode;
                this.SetProductMessage(string.Empty, string.Empty, currentBarCode);
            }

            return param;
        }

        private void qrExtantAmtPagination()
        {
            QueryParameter param = getSearchParameter();
            param.searchType = 3;
            param.currentPage = this.currentScanIndex;

            string strResult = ScannerWebServiceProxy.Instance.queryData(Converter.Serialize(param));
            BaseResult<QueryResult> serviceResult = Converter.Deserialize<BaseResult<QueryResult>>(strResult);

            if (serviceResult.IsSucessful())
            {
                this.RefreshScanGrid(Utils.Utils.GetTableStyle(typeof(QRStockInfo)), Utils.Utils.ConvertListToDataTable(serviceResult.ReturnValue.qrStockInfo)
                    , serviceResult.ReturnValue.ScanTotalPage, param.pageSize, serviceResult.ReturnValue.ScanTotalCount);
            }
            else
            {
                DispalyMessage(serviceResult.getReturnCodeMessage());
            }
        }

        private void batchExtantAmtPagination()
        {
            QueryParameter param = getSearchParameter();
            param.searchType = 2;
            param.currentPage = this.currentOrderIndex;

            string strResult = ScannerWebServiceProxy.Instance.queryData(Converter.Serialize(param));
            BaseResult<QueryResult> serviceResult = Converter.Deserialize<BaseResult<QueryResult>>(strResult);
            
            if (serviceResult.IsSucessful())
            {
                RefreshOrderGrid(Utils.Utils.GetTableStyle(typeof(ProductStockInfo)), Utils.Utils.ConvertListToDataTable(serviceResult.ReturnValue.productStockInfoList)
                    , serviceResult.ReturnValue.OrderTotalPage, param.pageSize, serviceResult.ReturnValue.OrderTotalCount);
            }
            else
            {
                DispalyMessage(serviceResult.getReturnCodeMessage());
                this.SetProductMessage(string.Empty, string.Empty, string.Empty, string.Empty);
                DispalyProductMessage();
            }
        }
    }
}